package xyz.lisys.action;

import com.opensymphony.xwork2.ActionSupport;
import lombok.val;
import org.apache.struts2.dispatcher.multipart.MultiPartRequestWrapper;
import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.interceptor.ServletResponseAware;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.util.Objects;

@Controller("inParameterAction")
@Scope("prototype")
public class InParameterAction extends ActionSupport implements ServletRequestAware, ServletResponseAware {
    private HttpServletRequest request;
    private HttpServletResponse response;

    private File upload;
    private String uploadContentType;
    private String uploadFileName;

    public String param1() {
        System.out.println("param1");
        return SUCCESS;
    }

    public String param2() {
        // 返回json格式数据
        try {
            PrintWriter out = response.getWriter();

            if ("GET".equalsIgnoreCase(request.getMethod())) {
                out.write("{\"name\":\"GET\"}");
            } else if ("POST".equalsIgnoreCase(request.getMethod())) {
                out.write("{\"name\":\"POST\"}");
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        }

        return NONE;
    }

    public String param3() throws IOException {
        val resourcePath = Objects.requireNonNull(getClass().getClassLoader().getResource(".")).getPath();
        val saveFile = new File(resourcePath, getUploadFileName()).getAbsoluteFile();

        FileInputStream fis = new FileInputStream(this.getUpload());
        FileOutputStream fos = new FileOutputStream(saveFile);

        byte[] buffer = new byte[1024];
        int length = fis.read(buffer);
        while (length > 0) {
            fos.write(buffer, 0, length);
            length = fis.read(buffer);
        }
        fis.close();
        fos.flush();
        fos.close();

        try {
            PrintWriter out = response.getWriter();
            out.write("{\"name\":\"upload success\"}");
        } catch (IOException e) {
            throw new RuntimeException(e);
        }

        return NONE;
    }

    public String param4() {
        val mRequest = (MultiPartRequestWrapper) request;
        mRequest.getFiles("upload");
        return NONE;
    }

    @Override
    public void setServletRequest(HttpServletRequest httpServletRequest) {
        this.request = httpServletRequest;
    }

    @Override
    public void setServletResponse(HttpServletResponse httpServletResponse) {
        this.response = httpServletResponse;
    }

    public File getUpload() {
        return upload;
    }

    public void setUpload(File upload) {
        this.upload = upload;
    }

    public String getUploadContentType() {
        return uploadContentType;
    }

    public void setUploadContentType(String uploadContentType) {
        this.uploadContentType = uploadContentType;
    }

    public String getUploadFileName() {
        return uploadFileName;
    }

    public void setUploadFileName(String uploadFileName) {
        this.uploadFileName = uploadFileName;
    }

}

